{% extends 'stocks/base.html' %}

{% block title %}{{ stock.symbol }} - 日交易数据{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>
        <i class="bi bi-bar-chart"></i> {{ stock.symbol }} - {{ stock.name }} 日交易数据
    </h2>
    <div class="btn-group">
        <a href="{% url 'stock_detail' stock.symbol %}" class="btn btn-outline-primary">
            <i class="bi bi-building"></i> 公司详情
        </a>
        <a href="{% url 'financial_data' stock.symbol %}" class="btn btn-outline-info">
            <i class="bi bi-calculator"></i> 财务指标
        </a>
        <a href="{% url 'stock_list' %}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> 返回列表
        </a>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h5><i class="bi bi-calendar"></i> 历史交易数据</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-sm table-hover">
                <thead class="table-light">
                    <tr>
                        <th>日期</th>
                        <th>开盘价</th>
                        <th>收盘价</th>
                        <th>最高价</th>
                        <th>最低价</th>
                        <th>成交量</th>
                        <th>成交额</th>
                        <th>涨跌幅</th>
                        <th>涨跌额</th>
                        <th>振幅</th>
                        <th>换手率</th>
                    </tr>
                </thead>
                <tbody>
                    {% for data in page_obj %}
                    <tr>
                        <td>{{ data.trade_date }}</td>
                        <td>{{ data.open|default:"-" }}</td>
                        <td>{{ data.close|default:"-" }}</td>
                        <td>{{ data.high|default:"-" }}</td>
                        <td>{{ data.low|default:"-" }}</td>
                        <td>{{ data.volume|default:"-" }}</td>
                        <td>
                            {% if data.amount %}
                                {{ data.amount|floatformat:2 }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td class="{% if data.change_pct and data.change_pct > 0 %}positive-change{% elif data.change_pct and data.change_pct < 0 %}negative-change{% endif %}">
                            {% if data.change_pct %}
                                {{ data.change_pct|floatformat:2 }}%
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td class="{% if data.change_amount and data.change_amount > 0 %}positive-change{% elif data.change_amount and data.change_amount < 0 %}negative-change{% endif %}">
                            {% if data.change_amount %}
                                {{ data.change_amount|floatformat:2 }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% if data.price_amplitude %}
                                {{ data.price_amplitude|floatformat:2 }}%
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>
                            {% if data.turnover_rate %}
                                {{ data.turnover_rate|floatformat:2 }}%
                            {% else %}
                                -
                            {% endif %}
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="11" class="text-center text-muted py-4">
                            <i class="bi bi-database display-4"></i>
                            <p class="mt-2">暂无交易数据</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if page_obj.paginator.num_pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?page=1">首页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
                </li>
                {% endif %}

                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                    </li>
                    {% endif %}
                {% endfor %}

                {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">末页</a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}

        <div class="text-muted text-center mt-3">
            显示 {{ page_obj.start_index }} - {{ page_obj.end_index }} 条，共 {{ page_obj.paginator.count }} 条记录
        </div>
    </div>
</div>

<div class="mt-4">
    <div class="alert alert-info">
        <i class="bi bi-info-circle"></i> 
        <strong>数据说明:</strong> 以上数据来源于MCP stock_db数据库的stock_daily表，包含股票的历史日交易数据。
    </div>
</div>
{% endblock %}
